package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.FgWorkoutsBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import com.link.play.fit.ai.ui.weightcontrol.ac.WeighControlPbAc
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class WorkoutsFg : BasePageFragment<FgWorkoutsBinding>(FgWorkoutsBinding::inflate) {

    lateinit var userGuideState: MutableStateFlow<UserGuide>


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val letterSpacing: Float = 1f / binding.tvTitle.textSize
        binding.tvTitle.letterSpacing = letterSpacing

        when (val ac = requireActivity()) {
            is GuidePbAc -> {
                userGuideState = ac.viewModel.userGuideState
            }

            is WeighControlPbAc -> {
                userGuideState = ac.viewModel.userGuideState
            }
        }


        lifecycleScope.launch {
            userGuideState.collectLatest {

                when (it.workouts) {
                    0 -> {
                        binding.ll02.isSelected = true
                        binding.ll35.isSelected = false
                        binding.ll6.isSelected = false
                    }

                    1 -> {
                        binding.ll35.isSelected = true
                        binding.ll02.isSelected = false
                        binding.ll6.isSelected = false
                    }

                    2 -> {
                        binding.ll6.isSelected = true
                        binding.ll35.isSelected = false
                        binding.ll02.isSelected = false
                    }

                    else -> {
                    }

                }
            }
        }
        binding.ll02.setOnClickListener {
            userGuideState.update {
                it.copy(workouts = 0)
            }
        }
        binding.ll35.setOnClickListener {
            userGuideState.update {
                it.copy(workouts = 1)
            }
        }
        binding.ll6.setOnClickListener {
            userGuideState.update {
                it.copy(workouts = 2)
            }
        }

    }

}